package com.webframe.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.webframe.dao.IJdbcDao;
import com.webframe.service.def.IHouseInfoRentPubService;

@Service
public class HouseInfoRentPubService implements IHouseInfoRentPubService
{
	@Autowired
	private IJdbcDao jdbcDao;

	@Override
	public List<Map<String, Object>> getDistrict(String pid)
	{
		String sql = "select id_,name_ from t_district where tid_=? order by id_";
		List<Map<String, Object>> list = jdbcDao.doQueryBySql(sql, pid);
		return list;
	}

	@Override
	public List<Map<String, Object>> getResidenceType(String id)
	{
		String sql = "select id_,residence_type_name_ name_  from t_residence_type t where 1=1";
		if(StringUtils.isNotEmpty(id))
		{
			sql += " and id_=?";
		}
		sql += " order by t.id_";
		List<Map<String, Object>> list;
		if(StringUtils.isEmpty(id))
		{
			list = jdbcDao.doQueryBySql(sql);
		}
		else
		{
			list = jdbcDao.doQueryBySql(sql, id);
		}
		return list;
	}

	@Override
	public List<Map<String, Object>> getAdornType(String id)
	{
		String sql = "select id_,adorn_type_name_ name_  from t_adorn_type t where 1=1";
		if(StringUtils.isNotEmpty(id))
		{
			sql += " and id_=?";
		}
		sql += " order by t.id_";
		List<Map<String, Object>> list;
		if(StringUtils.isEmpty(id))
		{
			list = jdbcDao.doQueryBySql(sql);
		}
		else
		{
			list = jdbcDao.doQueryBySql(sql, id);
		}
		return list;
	}

	@Override
	public List<Map<String, Object>> getDirection(String id)
	{
		String sql = "select id_,direction_name_ name_  from t_direction t where 1=1";
		if(StringUtils.isNotEmpty(id))
		{
			sql += " and id_=?";
		}
		sql += " order by t.id_";
		List<Map<String, Object>> list;
		if(StringUtils.isEmpty(id))
		{
			list = jdbcDao.doQueryBySql(sql);
		}
		else
		{
			list = jdbcDao.doQueryBySql(sql, id);
		}
		return list;
	}

	@Override
	public List<Map<String, Object>> getHouseConfig(String id)
	{
		String sql = "select id_,house_config_name_ name_  from t_house_config t where 1=1";
		if(StringUtils.isNotEmpty(id))
		{
			sql += " and id_=?";
		}
		sql += " order by t.order_";
		List<Map<String, Object>> list;
		if(StringUtils.isEmpty(id))
		{
			list = jdbcDao.doQueryBySql(sql);
		}
		else
		{
			list = jdbcDao.doQueryBySql(sql, id);
		}
		return list;
	}

	@Override
	public List<Map<String, Object>> getPayType(String id)
	{
		String sql = "select id_,pay_type_name_ name_  from t_pay_type t where 1=1";
		if(StringUtils.isNotEmpty(id))
		{
			sql += " and id_=?";
		}
		sql += " order by t.id_";
		List<Map<String, Object>> list;
		if(StringUtils.isEmpty(id))
		{
			list = jdbcDao.doQueryBySql(sql);
		}
		else
		{
			list = jdbcDao.doQueryBySql(sql, id);
		}
		return list;
	}

	@Override
	public List<Map<String, Object>> getXiaoqu(String keyWord, String cityId)
	{
		List<Map<String, Object>> resList = new ArrayList<Map<String,Object>>();
		String sql = "call findLChild(?,?)";
		String sql2 = "select * from t_community t where (t.community_name_ like ? or t.py_ like ? or t.py_head_ like ?) and t.city_id_=?";
		List<Map<String, Object>> list = jdbcDao.doQueryBySql(sql, cityId, 2);
		List<Map<String, Object>> list2 = jdbcDao.doQueryBySql(sql2, "%"+keyWord+"%", keyWord+"%", keyWord+"%", cityId);
		for (Iterator<Map<String, Object>> iterator = list2.iterator(); iterator.hasNext();)
		{
			Map<String, Object> map = iterator.next();
			int businessId =  Integer.parseInt(map.get("business_id_").toString());
			for (Iterator<Map<String, Object>> iterator2 = list.iterator(); iterator2.hasNext();)
			{
				Map<String, Object> map2 = iterator2.next();
				int id =  Integer.parseInt(map2.get("id_").toString());
				if(id == businessId)
				{
					map.putAll(map2);
					map.put("communityId", map.get("id_"));
					resList.add(map);
					break;
				}
			}
		}
		return resList;
	}
	
	
}
